<?php
  require_once("../includes/config.php"); 
  
  if(isset($_POST['add-periods'],$_POST['pidz']) && $_POST['add-periods'] == 1)
  {
  	if ($_POST['pidz']!="")
	{
	$pidz=explode("|",$_POST['pidz']);
	foreach ($pidz as $pid)
	{
	$from="from-".$pid;
	$to="to-".$pid;
	$del="del-".$pid;	
	if (isset($_POST["$from"],$_POST["$to"])&&!isset($_POST["$del"]))
	{
	$result = query("UPDATE periods SET period_from = ?, period_to = ? where period_id=? and owner_id=?",$_POST["$from"],$_POST["$to"],$pid,$_SESSION['id']);     
	}
	elseif(isset($_POST["$del"]))
	{
	delperiod($pid,$_SESSION['id']);
	}
	}
	    $_SESSION['success_message'] = "تم تحديث فترات الايجار";
  }
  
  }
  
  if(isset($_POST['value']))
  {
	//Updating the specific value updated by user either daily weekly monthly
	if (isset($_POST['field']))
	setpp($_POST['id'],$_POST['pid'],$_POST['field'],$_POST['value']);
	else
	setmd($_POST['id'],$_POST['pid'],$_POST['value']);
    echo $_POST['value'];
    die();
  }
  
  if (isset($_POST['newperiod']))
  {
  query("INSERT INTO `periods` (`owner_id`,`apt`, `period_from`, `period_to`) VALUES (?,NULL, '', '')",$_SESSION['id']);
  $_SESSION['success_message'] = "تم اضافة فترة جديدة";
  }
  
  $periods = query("SELECt * from periods where owner_id=?",$_SESSION['user_id']);
  $aparts = query("SELECT * from apartments where owner_id = ?",$_SESSION['user_id']);
  render("settings/apartments-price.php",array("title"=>"إعدادات الاسعار","periods"=>$periods,"aparts"=>$aparts));
  ?>